﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Core;
using MvcEngine.Core.Extensions;
using MvcEngine.Core.Utils;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core.Helpers;
using MvcEngine.Mvc.ModelBinders;
using System.Globalization;
using MvcEngine.Core.Localization;

namespace MvcEngine.Blog.ModelBinders
{
    public class BlogPostModelBinder : ModelBinderBase
    {
        public override object Bind(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request = controllerContext.HttpContext.Request;
            
            BlogPost post = IoC.Resolve<BlogPost>();
            post.Title = request.Form.Get("Title");
            post.PostContent = request.Form.Get("PostContent");
            post.Slug = request.Form.Get("Slug");

            if (string.IsNullOrEmpty(post.Slug))
                post.Slug = UrlUtils.RemoveIllegalCharacters(post.Title);
            else
                post.Slug = UrlUtils.RemoveIllegalCharacters(post.Slug);

            post.Description = request.Form.Get("Description");
            post.Keywords = request.Form.Get("Keywords");
            post.Author = request.Form.Get("Author");

            post.IsCommentEnabled = request.Form.IsTrue("IsCommentEnabled");
            post.ModifiedDate = DateTime.Now;

            string pubDate = request.Form.Get("PublishedDate");
            if (string.IsNullOrEmpty(pubDate))
            {
                post.PublishedDate = null;
            }
            else
            {
                DateTime time = DateTime.MinValue;
                string pubTime = request.Form.Get("PublishedTime");
                if (!string.IsNullOrEmpty(pubTime))
                {
                    if (!DateTime.TryParse(pubTime, CultureInfo.CurrentCulture, DateTimeStyles.None, out time))
                    {
                        var modelState = controllerContext.Controller.ViewData.ModelState;
                        modelState.AddModelError("PublishedTime", ResourceReader.Current.GetString("InvalidTime"));
                        time = DateTime.MinValue;
                    }
                }

                DateTime date;
                if (DateTime.TryParse(pubDate, CultureInfo.CurrentCulture, DateTimeStyles.None, out date))
                {
                    post.PublishedDate = date;
                    if (time != DateTime.MinValue) 
                    {
                        post.PublishedDate = post.PublishedDate.Value.AddHours(time.Hour);
                        post.PublishedDate = post.PublishedDate.Value.AddMinutes(time.Minute);
                    }
                }
                else 
                {
                    var modelState = controllerContext.Controller.ViewData.ModelState;
                    modelState.AddModelError("PublishedDate", ResourceReader.Current.GetString("InvalidDate"));   
                }
            }

            Validate(post, controllerContext);
            return post;
        }
    }
}
